package com.company.admin.service.news;

import com.company.admin.common.myException.BizException;
import com.company.admin.common.util.StrAndListUtil;
import com.company.admin.dao.file.FileDao;
import com.company.admin.dao.news.NewsDao;
import com.company.admin.dao.news.NewsDetailDao;
import com.company.admin.domain.news.CompanyNewsDomain;
import com.company.admin.entity.file.AttachFileEntity;
import com.company.admin.entity.news.CompanyNewsDetailEntity;
import com.company.admin.entity.news.CompanyNewsEntity;
import com.company.admin.req.news.CompanyNewsAddRequest;
import com.company.admin.req.news.CompanyNewsDeleteRequest;
import com.company.admin.req.news.CompanyNewsListRequest;
import com.company.admin.req.news.CompanyNewsModifyRequest;
import com.company.admin.resp.news.CompanyNewsListResponse;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author 赵成林
 * @date 2019/2/17 21:14
 */
@Service
public class CompanyNewsService {

    @Autowired
    private NewsDao newsDao;
    @Autowired
    private NewsDetailDao newsDetailDao;
    @Autowired
    private FileDao fileDao;

    @Transactional
    public String add(CompanyNewsAddRequest request){
        if (StringUtils.isBlank(request.getNewsTitle())){
            throw new BizException(205, "新闻标题不可以为空");
        }
        CompanyNewsEntity entity = new CompanyNewsEntity();
        entity.setCreateTime(new Date());
        entity.setDetailInfo(request.getDetailInfo());
        entity.setMainInfo(request.getMainInfo());
        List<Long> pictureIds = request.getNewsPictureIds();
        String ids = StrAndListUtil.listToStr(pictureIds);
        entity.setNewsPictureIds(ids);
        entity.setNewsTitle(request.getNewsTitle());
        entity.setUpdateTime(new Date());
        newsDao.insert(entity);
        CompanyNewsDetailEntity detailEntity = new CompanyNewsDetailEntity();
        detailEntity.setCompanyNewsId(entity.getCompanyNewsId());
        detailEntity.setDetailInfo(request.getDetailInfo());
        newsDetailDao.insert(detailEntity);
        return "success";
    }

    @Transactional
    public String modify(CompanyNewsModifyRequest request){
        if (request.getCompanyNewsId() == null){
            throw new BizException(205, "请先选择新闻");
        }
        CompanyNewsEntity entity = newsDao.getById(request.getCompanyNewsId());
        if (entity == null){
            throw new BizException(205, "新闻不存在, id是 " + request.getCompanyNewsId());
        }
        entity.setNewsTitle(request.getNewsTitle());
        entity.setNewsPictureIds(StrAndListUtil.listToStr(request.getNewsPictureIds()));
        entity.setMainInfo(request.getMainInfo());
        entity.setDetailInfo(request.getDetailInfo());
        entity.setCompanyNewsId(request.getCompanyNewsId());
        entity.setUpdateTime(new Date());
        newsDao.modify(entity);

        if (StringUtils.isNotBlank(request.getDetailInfo())){
            CompanyNewsDetailEntity detailEntity = new CompanyNewsDetailEntity();
            detailEntity.setDetailInfo(request.getDetailInfo());
            detailEntity.setCompanyNewsId(request.getCompanyNewsId());
            newsDetailDao.modify(detailEntity);
        }
        return "success";
    }

    public String delete(CompanyNewsDeleteRequest request){
        if (CollectionUtils.isEmpty(request.getCompanyNewsIds())){
            throw new BizException(205, "请选择要删除的新闻");
        }

        newsDao.deleteByIds(request.getCompanyNewsIds());
        return "success";
    }

    public CompanyNewsListResponse list(CompanyNewsListRequest request){
        if (request.getOffset() == null || request.getLength() == null){
            throw new BizException(205,"分页参数不完整");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("offset", request.getOffset());
        map.put("length", request.getLength());
        List<CompanyNewsEntity> companyNewsEntities = newsDao.list(map);
        Long totalCount = newsDao.totalCount();
        CompanyNewsListResponse response = new CompanyNewsListResponse();
        response.setTotalCount(totalCount);
        List<CompanyNewsDomain> domains = new ArrayList<>();
        for (CompanyNewsEntity entity : companyNewsEntities){
            CompanyNewsDomain domain = new CompanyNewsDomain();
            String pictureids = entity.getNewsPictureIds();
            List<Long> ids = StrAndListUtil.strToList(pictureids);
            List<AttachFileEntity> attachFileEntities = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(ids)){
                attachFileEntities = fileDao.selectByIds(ids);
            }
            domain.setAttachFileEntities(attachFileEntities);
            domain.setCompanyNewsId(entity.getCompanyNewsId());
            domain.setCreateTime(entity.getCreateTime());
            domain.setDetailInfo(entity.getDetailInfo());
            domain.setMainInfo(entity.getMainInfo());
            domain.setNewsTitle(entity.getNewsTitle());
            domain.setUpdateTime(entity.getUpdateTime());
            domains.add(domain);
        }

        response.setCompanyNewsDomains(domains);
        return response;
    }
}
